﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using NicoView.Model.Speech;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using NicoView.Model;
using NicoView.Model.Service.Comments;
using NicoView.Model.Service.Property;
using NicoView.ViewModel.Message;
using NicoView.Model.Browser;
using NicoView.Model.Share.Browser;
using NicoView.Model.Share.Speech;

namespace NicoView.ViewModel
{
    public sealed class NicoViewSettingConsoleViewModel : NicoViewViewModelBase
    {
        public NicoViewSettingConsoleViewModel()
        {


            this.authSetCommand = null;
            

            this.CommentNgShareLevels = new ObservableCollection<CommentNgShareLevelModel>();

            foreach (var ngShareLevel in CommentNgShareLevelModel.GetNgShareLevels())
            {
                this.CommentNgShareLevels.Add(ngShareLevel);
            }


            this.commentNgShareLevel = null;
        }

        public void Loded()
        {
            
            if (!IsInDesignMode)
            {
                InternalApplication.Instance.Property.PropertyChanged += (s, e) =>
                {
                    RaisePropertyChanged(e.PropertyName);

                };

                NicoViewSpeacherApplication.Current.PropertyChanged += (s, e) =>
                {
                    RaisePropertyChanged(e.PropertyName);
                };

                Browsers.Instance.PropertyChanged += (s, e) =>
                {
                    RaisePropertyChanged(e.PropertyName);
                };

                InternalApplication.Instance.PropertyChanged += (s, e) =>
                {
                    RaisePropertyChanged(e.PropertyName);
                };
                InternalApplication.Instance.SpeechApplication.PropertyChanged += (s, e) =>
                {
                    RaisePropertyChanged(e.PropertyName);
                };

                Browsers.Instance.RaisePropertyChangedAll();
                InternalApplication.Instance.Property.RaisePropertyChangedAll();
                NicoViewSpeacherApplication.Current.RaisePropertyChangedAll();
                InternalApplication.Instance.RaisePropertyChangedAll();
                InternalApplication.Instance.SpeechApplication.RaisePropertyChangedAll();
            }
            
        }


        public ObservableCollection<CommentNgShareLevelModel> CommentNgShareLevels { get; private set; }


        public bool VisibleCommentArtComment
        {
            get
            {
                return this.InternalApplication.Property.VisibleCommentArtComment;
            }

            set
            {
                this.InternalApplication.Property.VisibleCommentArtComment = value;
            }
        }

        public bool VisibleAnonymous
        {
            get
            {
                return this.InternalApplication.Property.VisibleAnonymous;
            }

            set
            {
                this.InternalApplication.Property.VisibleAnonymous = value;
            }
        }

        public bool SpeechEnable
        {
            get
            {
                return InternalApplication.Instance.SpeechEnable;
            }
            set
            {
                InternalApplication.Instance.SpeechEnable = value;
            }

        }

        public bool AutoCreateNextBroadcast
        {
            get
            {
                return this.InternalApplication.Property.AutoCreateNextBroadcast;
            }

            set
            {
                this.InternalApplication.Property.AutoCreateNextBroadcast = value;
            }


        }


        public bool AnonymousSend
        {
            get
            {
                return this.InternalApplication.Property.AnonymousSend;
            }

            set
            {
                this.InternalApplication.Property.AnonymousSend = value;
            }
        }

        public bool OwnerSend
        {
            get
            {
                return this.InternalApplication.Property.OwnerSend;
            }

            set
            {
                this.InternalApplication.Property.OwnerSend = value;
            }
        }

        private CommentNgShareLevelModel commentNgShareLevel;

        public CommentNgShareLevelModel CommentNgShareLevel
        {

            get
            {
                if (this.commentNgShareLevel == null)
                {
                    var commentNgLevels = from commentNgLevel in this.CommentNgShareLevels
                                          where this.InternalApplication.Instance.Property.CommentNgShareLevel == commentNgLevel.NgShareLevel
                                          select commentNgLevel;

                    if (commentNgLevels.Count() > 0)
                    {
                        this.commentNgShareLevel = commentNgLevels.First();
                    }

                }

                return this.commentNgShareLevel;
               
            }
            set
            {
                this.commentNgShareLevel = value;
                this.InternalApplication.Instance.Property.CommentNgShareLevel = value.NgShareLevel;
            }
        }

        public bool UsingBroadcasatEncoder
        {
            get
            {
                return this.InternalApplication.Property.UsingBroadcasatEncoder;
            }
            set
            {
                this.InternalApplication.Property.UsingBroadcasatEncoder = value;
            }
        }


        

        

        public bool NextFrameAutoConnection
        {
            get
            {
                return InternalApplication.Property.NextFrameAutoConnection;
            }

            set
            {
                InternalApplication.Property.NextFrameAutoConnection = value;
            }
        }

        public bool NextFrameAutoConnectionWithBrowser
        {
            get
            {
                return InternalApplication.Property.NextFrameAutoConnectionWithBrowser;
            }
            set
            {
                InternalApplication.Property.NextFrameAutoConnectionWithBrowser = value;
            }
        }

        public bool VisibleCustomNameOnly
        {
            get
            {

                return InternalApplication.Instance.Property.VisibleCustomNameOnly;
            }

            set
            {
                InternalApplication.Instance.Property.VisibleCustomNameOnly = value;
            }
        }

        public bool SpeechLearnEnable
        {
            get
            {
                return InternalApplication.Instance.Property.SpeechLearnEnable;
            }
            set
            {
                InternalApplication.Instance.Property.SpeechLearnEnable = value;
            }
        }


        public bool NgAvoidance
        {
            get
            {
                return InternalApplication.Instance.Property.NgAvoidance;
            }
            set
            {
                InternalApplication.Instance.Property.NgAvoidance = value;
            }

        }

        public bool AtMarkNameToCustomName
        {
            get
            {
                return InternalApplication.Instance.Property.AtMarkNameToCustomName;
            }
            set
            {
                InternalApplication.Instance.Property.AtMarkNameToCustomName = value;
            }
        }


        public ISpeacherProvider CurrentSpeacherProvider
        {
            get
            {

                return InternalApplication.Instance.SpeechApplication.CurrentSpeacherProvider;
            }

            set
            {

               InternalApplication.Instance.SpeechApplication.CurrentSpeacherProvider = value;
            }

        }


        public INicoViewBrowser CurrentUrlExecuteBrowser
        {
            get
            {
                return Browsers.Instance.CurrentUrlExecuteBrowser;
            }
            set
            {
                Browsers.Instance.CurrentUrlExecuteBrowser = value;
            }
        }

        public ICollection<INicoViewBrowser> UrlExecuteBrowsers
        {
            get
            {
                return Browsers.Instance.UrlExecuteBrowsers;
            }
        }

        public ICollection<ISpeacherProvider> Providers
        {
            get
            {
                return NicoViewSpeacherApplication.Current.ProviderList;
            }


        }

        private ICommand authSetCommand;
        public ICommand AuthSetCommand
        {
            get
            {
                if (this.authSetCommand == null) this.authSetCommand = new RelayCommand(async() =>
                {
                    await InternalApplication.Instance.CurrentAuthSetRequest();
                    
                });

                return this.authSetCommand;
            }
        }

        
        
    }
}
